package com.jetpack.mvvm.lib.binding.ext

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import java.lang.reflect.Method

const val INFLATE_NAME = "inflate"
const val BIND_NAME = "bind"

fun <T> Class<T>.inflateMethod(): Method = getMethod(INFLATE_NAME, LayoutInflater::class.java)

/**
 * 自定义 view 时，layout 的最外层 tag 不是 merge，自定义 view 与 layout 文件最终为嵌套方式，
 * viewBinding 文件 inflate 方法为：inflate(@NonNull LayoutInflater inflater,@NonNull ViewGroup parent, boolean attachToParent)
 */
fun <T> Class<T>.inflateMethodWithViewGroup(): Method =
    getMethod(INFLATE_NAME, LayoutInflater::class.java, ViewGroup::class.java, Boolean::class.java)

/**
 * 自定义 view 时，采用 merge 来定义 layout 的最外层，viewBinding 文件 inflate 方法为：inflate(@NonNull LayoutInflater inflater,
@NonNull ViewGroup parent)
 */
fun <T> Class<T>.inflateMethodWithViewGroupWithMerge(): Method =
    getMethod(INFLATE_NAME, LayoutInflater::class.java, ViewGroup::class.java)

fun <T> Class<T>.bindMethod(): Method = getMethod(BIND_NAME, View::class.java)